package cn.com.shbs.Service.CustomerService;

import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.Dao.Customer.CustomerDao;
import cn.com.shbs.Dao.CustomerMedical.CustomerMedicalDao;
import cn.com.shbs.model.Customer;
import cn.com.shbs.model.CustomerMedical;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
public class CustomerServiceImpl implements ICustomerService{

    static Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
    @Autowired
    CustomerDao customerDao;
    @Autowired
    CustomerMedicalDao customerMedicalDao;
    /**
     * 添加客户信息，客户基本医疗信息
     * @param customer 客户信息
     * @param customerMedical 客户基本医疗信息
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addcustomer(Customer customer, CustomerMedical customerMedical) throws Exception{
        logger.info("CustomerServiceImpl.addcustomer() 方法 start");
        try{
            //添加客户信息
            customerDao.addcustomer(customer);
            if(customerMedical!=null&&
                    customerMedical.getCustomer_code()!=null&&
                    !"".equals(customerMedical.getCustomer_code().trim())){
                //添加客户基本医疗信息
                customerMedicalDao.addcustomerMedical(customerMedical);

            }


        }catch (Exception e){
            logger.error("CustomerServiceImpl.addcustomer()",e);
            throw  e;
        }
        logger.info("CustomerServiceImpl.addcustomer() 方法 end");

    }

    /**
     * 修改客户信息，客户基本医疗信息
     * @param customer
     * @param customerMedical
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateCustomer(Customer customer, CustomerMedical customerMedical) throws Exception {
        logger.info("CustomerServiceImpl.updateCustomer() 方法 start");

        try{

            if(customer!=null
                    &&customer.getCustomer_code()!=null
                    &&!"".equals(customer.getCustomer_code().trim())){
                customer.setCreattime(CommonUtil.getNowTime_tamp());
                //修改用户信息
                customerDao.updateCustomer(customer);

            }
            if(customerMedical!=null
                    &&customerMedical.getCustomer_code()!=null
                    &&!"".equals(customerMedical.getCustomer_code().trim())){
                //修改用户信息
                customerMedicalDao.updateCustomerMedical(customerMedical);
            }



        }catch (Exception e){
            logger.error("CustomerServiceImpl.updateCustomer()",e);
            throw  e;
        }

        logger.info("CustomerServiceImpl.updateCustomer() 方法 end");
    }

}
